// ====================================================
//
// This file is part of the Cscec81 Passport Base Platform.
//
// Create by Cscec81 <support@cscec81.com>
// Copyright (c) 2020-2025 cscec81.com
//
// ====================================================

package com.cscec81.passport.base.data.jpa.utils;

import org.springframework.data.domain.Page;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings({"unchecked", "all"})
public class PageUtils extends cn.hutool.core.util.PageUtil {

  /**
   * List 分页
   */
  public static List<Object> toPage(int page, int size, List<Object> list) {
    int fromIndex = page * size;
    int toIndex = page * size + size;
    if (fromIndex > list.size()) {
      return new ArrayList<>();
    } else if (toIndex >= list.size()) {
      return list.subList(fromIndex, list.size());
    } else {
      return list.subList(fromIndex, toIndex);
    }
  }

  /**
   * Page 数据处理，预防redis反序列化报错
   */
  public static Map<String, Object> toPage(Page page) {
    Map<String, Object> map = new LinkedHashMap<>(2);
    map.put("content", page.getContent());
    map.put("totalElements", page.getTotalElements());

    return map;
  }

  /**
   * 自定义分页
   */
  public static Map<String, Object> toPage(Object object, Object totalElements) {
    Map<String, Object> map = new LinkedHashMap<>(2);
    map.put("content", object);
    map.put("totalElements", totalElements);

    return map;
  }
}
